import { Component } from '@angular/core';
import { RPaginatorArgv, RQuest, RTable, RTableColumns } from '../../r-models/r-table.model';
import { RTableService } from '../../r-service/r-table.service';

@Component({
  selector: 'oy-r-common-list',
  template: ''
})

/**
 * 公司：xxx有限公司
 * 版权：xxx2020
 * 描述：表格列表公共配置模块TS
 * 作者：oyjl
 * 时间：2020-03-13 21:08:54
 */
export class RCommonListComponent {

  public rTable: RTable;
  public innerCacheData: any; // 组件内部缓存查询数据

  constructor(public rTableService: RTableService) { }

  /**
   * 描述：表格列表公共配置列表数据查询处理,status为默认值true，就从新设置分页第一条索引为0，如果为false就不做重置
   * 作者：oyjl
   * 时间：2020-03-13 21:22:52
   */
  public searchDataHandle(data?: RQuest, paginatorArgv?: RPaginatorArgv) {
    if (data && paginatorArgv) {
      this.innerCacheData = {
        data,
        paginatorArgv
      };
    }
    this.rTableService.sendPost(this.rTable, this.innerCacheData.data.req, this.innerCacheData.data.url,
      this.innerCacheData.paginatorArgv || this.rTableService.paginatorArgv).subscribe((res) => {
        if (this.rTableService.afterStatusSwitch(this.rTable.prop.dataConfig, res)) {
          this.rTable.prop.value = this.rTableService.afterDataSwitch(this.rTable.prop.dataConfig, res);
          this.switchBoolean(this.rTable.prop.value, this.rTable.prop.columns);
          this.rTable.prop.selection = [];
        } else {
          this.rTableService.message('error', res.opMsg || res.msg);
        }
      }, (err) => {
        this.rTableService.message('error', err.message);
      });
  }

  /**
   * 描述：表格列表公共配置操作函数，结束后会重新调用调用查询方法，更新数据
   * 作者：oyjl
   * 时间：2020-03-13 21:17:43
   */
  public operateMethod(data) {
    data.service(data.req).subscribe((res: any) => {
      if (this.rTableService.afterStatusSwitch(this.rTable.prop.dataConfig, res)) {
        this.searchDataHandle(this.innerCacheData.data, this.innerCacheData.paginatorArgv);
        this.rTableService.message('info', '成功');
      } else {
        this.rTableService.message('error', res.opMsg || res.msg);
      }
    }, (err) => {
      this.rTableService.message('error', err.message);
    });
  }

  /**
   * 描述：获取token,先从cookie中获取，再从storage中获取
   * 作者：oyjl
   * 时间：2020-08-21 18:17:28
   */
  public getCookie(name) {
    let arr;
    const reg = new RegExp(name + '=([^;]*)');
    arr = document.cookie.match(reg) || sessionStorage.getItem(name);
    if (arr) {
      if (arr instanceof Array) {
        return unescape(arr[1]);
      } else {
        return arr;
      }
    } else {
      return null;
    }
  }

  /**
   * 描述：开关组件状态转换由非boolean转换为boolean,使用引用类型
   * 作者：oyjl
   * 时间：2021-07-16 14:51:17
   */
  public switchBoolean(rows: Array<object>, cols: Array<RTableColumns>): void {
    const switchField = [];
    cols.forEach((e: RTableColumns) => {
      if (e.type === 'sw') {
        switchField.push(e.field);
      }
    });
    rows.forEach((e: any) => {
      switchField.forEach((m: string) => {
        if (e[m] === 0 || e[m] === '0') {
          e[m] = false;
        } else {
          e[m] = true;
        }
      });
    });
  }
}
